package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.mapper.DExpressCompanyMapper;
import com.igeekhome.pojo.DExpressCompany;
import com.igeekhome.pojo.DExpressCompanyExample;
import com.igeekhome.service.IExpressCompanyService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Romeo
 */
@Service("expressCompanyService")
public class ExpressCompanyServiceImpl implements IExpressCompanyService {
    
    @Autowired
    private DExpressCompanyMapper dExpressCompanyMapper;
    
    
    @Override
    public TableResultBean list(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        DExpressCompanyExample example = new DExpressCompanyExample();
        example.createCriteria().andStatusEqualTo(1);
        List<DExpressCompany> dExpressCompanies = dExpressCompanyMapper.selectByExample(example);
        PageInfo<DExpressCompany> pageInfo = new PageInfo<>(dExpressCompanies);
        if(dExpressCompanies != null){
            return TableResultBean.ok(pageInfo.getTotal(),dExpressCompanies);
        }
        return TableResultBean.fail("暂无数据");
    }

    @Override
    public ResultBean add(DExpressCompany expressCompany) {
        expressCompany.setStatus(1);
        int insert = dExpressCompanyMapper.insert(expressCompany);
        if(insert > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("新增失败");
    }

    @Override
    public ResultBean edit(DExpressCompany expressCompany) {
        int update = dExpressCompanyMapper.updateByPrimaryKeySelective(expressCompany);
        if(update > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("修改失败");
    }

    @Override
    public ResultBean logicDelete(String idsStr) {
        if(idsStr.contains(",")){
            idsStr = idsStr.substring(0,idsStr.length()-1);
        }
        if(!idsStr.contains(",")){
            // 单个删除
            DExpressCompany company = new DExpressCompany();
            company.setStatus(0);
            DExpressCompanyExample example = new DExpressCompanyExample();
            example.createCriteria().andExpressCompanyIdEqualTo(Integer.parseInt(idsStr));
            int delete = dExpressCompanyMapper.updateByExampleSelective(company, example);
            if(delete > 0){
                return ResultBean.success();
            }
        }else{
            // 批量删除
            int delete = dExpressCompanyMapper.deleteByIdStr(idsStr);
            if(delete > 0){
                return ResultBean.success();
            }
        }
        return ResultBean.fail("删除失败");
    }

    @Override
    public List<DExpressCompany> select() {
        DExpressCompanyExample example = new DExpressCompanyExample();
        example.createCriteria().andStatusEqualTo(1);
        return dExpressCompanyMapper.selectByExample(example);
    }
}
